/**
 * 
 */
$(document).ready(function($){
	
	var action = "../../general/modules/requests/getAll";
	var parameters = "";
	
	getData(action, parameters).done(function(data){
		
		$("table tbody").empty();
		
		$(data).each(function(i, item){
			
			$("table tbody").append(
				"<tr>" +
				"<td><span>" + item.id + "</span></td>" +
				"<td><span>" + item.montoTotal + "</span></td>" +
				"<td><span>" + item.cliente.nombres + "</span></td>" +
				"<td><span><a class='geol' href='#' data-lati='" + item.latitud + "' data-long='" + item.altitud + "'><span class='icon-location'></span></a></span></td>" +
				"<td><span><a class='rowo' href='#'><span class='icon-more'></span></a></span></td>" +
				"</tr>"
			);
			
		});
		
		$(".geol").on("click", function(e){
			
			var element = $(this);
			
			$(".main .body .loca").show();
			
			var latitude = parseFloat(element.attr("data-lati"));
			var longitude = parseFloat(element.attr("data-long"));
			var location = new google.maps.LatLng(latitude, longitude);
			
			var mapOptions = {
				zoom: 17, 
				center: location
			};
			
			var map = new google.maps.Map(document.getElementById("mapa"), mapOptions);

			var marker = new google.maps.Marker({
				position: location,
				map: map,
				title: "Destino del pedido"
			});
			
		});
		
		$(".rowo").on("click", function(e){
			
			e.preventDefault();
			
			$(".main .body .cont .data ul").css({
				"top" : $(this).position().top + "px",
				"left": ($(this).position().left - $(".main .body .cont .data ul").width()) + "px",
				"display":"block"
			});
			
		});
		
		$("table tfoot tr td span").empty().html(data.length.toString() + " registros listados...");
		
	});
	
});

$(window).keyup(function(e){
	
	if(e.which == 27){
		
		$(".main .body .loca").hide();
		
	};
	
});

function getData(url, parameters){
	
	return $.ajax({
		type: "POST",
		url: url,
		data: parameters,
		dataType: "json"
	});
	
}